package com.gest.util;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

public class dd extends JPanel {

	private BufferedImage image;

	public dd() {
		// this.loadImage("2.png");
	}

	public void loadImage(String filename) {
		try {
			this.image = ImageIO.read(new File(filename));
			this.repaint();
		} catch (IOException ex) {
			// handle exception...
		}
	}

	@Override
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		if (image != null) {
			// Scale it by width
			int scaledWidth = (int) ((image.getWidth() * getHeight() / image
					.getHeight()));
			// If the image is not off the screen horizontally...
			if (scaledWidth < getWidth()) {
				// Center the left and right destination x coordinates.
				int leftOffset = getWidth() / 2 - scaledWidth / 2;
				int rightOffset = getWidth() / 2 + scaledWidth / 2;
				g.drawImage(image, leftOffset, 0, rightOffset, getHeight(), 0,
						0, image.getWidth(), image.getHeight(), null);
			}
			// Otherwise, the image width is too much, even scaled
			// So we need to center it the other direction
			else {
				int scaledHeight = (image.getHeight() * getWidth())
						/ image.getWidth();
				int topOffset = getHeight() / 2 - scaledHeight / 2;
				int bottomOffset = getHeight() / 2 + scaledHeight / 2;
				g.drawImage(image, 0, topOffset, getWidth(), bottomOffset, 0,
						0, image.getWidth(), image.getHeight(), null);
			}
		}
	}
}